package com.msk.common.utils;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.*;

/**
 * 日期时间工具类
 *
 * @author 张琳飞
 *
 */
public class DateUtil {
	private static final Logger log = LoggerFactory.getLogger(DateUtil.class);
	private static final FastDateFormat datetimeFormat = FastDateFormat.getInstance(
			"yyyy-MM-dd HH:mm:ss");
	private static final FastDateFormat datetimeMinuteFormat = FastDateFormat.getInstance(
			"yyyy-MM-dd HH:mm");
	private static final FastDateFormat dateFormat = FastDateFormat.getInstance(
			"yyyy-MM-dd");
	private static final FastDateFormat dateFormatStr = FastDateFormat.getInstance(
			"yyyy年MM月dd日");
	private static final FastDateFormat dayFormat = FastDateFormat.getInstance(
			"MM-dd");
	private static final FastDateFormat dateDayFormat = FastDateFormat.getInstance(
			"yyyy-MM");
	private static final FastDateFormat timeFormat = FastDateFormat.getInstance(
			"HH:mm:ss");
	private static final FastDateFormat minuteFormatTime = FastDateFormat.getInstance(
			"HH:mm");
	private static final FastDateFormat timeFormatMOHDay = FastDateFormat.getInstance(
			"yyyy-MM-dd HH:mm");

	private static final FastDateFormat timeFormatMOHDayHourMinute = FastDateFormat.getInstance(
			"MM-dd HH:mm");

	private static final FastDateFormat filetimeFormat = FastDateFormat.getInstance(
			"yyyyMMddHHmmssSSS");
	private static final FastDateFormat nowTimeFormat = FastDateFormat.getInstance(
			"yyyyMMddHHmmss");

  private static final FastDateFormat nowDateFormatYyyy = FastDateFormat.getInstance(
    "yyyy");
  private static final FastDateFormat nowDateFormatYyyyMm = FastDateFormat.getInstance(
    "yyyyMM");
	private static final FastDateFormat nowDateFormat = FastDateFormat.getInstance(
			"yyyyMMdd");
  private static final FastDateFormat nowDateFormatYy = FastDateFormat.getInstance(
    "yyMMdd");
  private static final FastDateFormat nowDateFormatY = FastDateFormat.getInstance(
    "yMMdd");
	private static final FastDateFormat timeFormatYmdHm = FastDateFormat.getInstance(
			"yyyy/MM/dd HH:mm");
	private static final FastDateFormat onlyTimeFormat = FastDateFormat.getInstance(
			"HH:mm:ss");
	private static final FastDateFormat chineseAbbreviationsDatetimeFormat = FastDateFormat.getInstance(
			"yyyy年MM月dd日 HH:mm");
	private static final String[] dayOfWeek = new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

	private static final FastDateFormat chineseAbbreviationsDatetimeFormatMdHm = FastDateFormat.getInstance(
			"MM月dd日 HH:mm");
	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String currentDatetime() {
		return datetimeFormat.format(now());
	}
	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyy/MM/dd HH:mm
	 *
	 * @return
	 */
	public static String currentDatetimeYmdHm() {
		return timeFormatYmdHm.format(now());
	}

	/**
	 * 获得当前月
	 * <p>
	 * 日期时间格式yyyy-MM
	 *
	 * @return
	 */
	public static String dateDayFormat() {
		return dateDayFormat.format(now());
	}

	/**
	 * 获得当前月
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm
	 *
	 * @return
	 */
	public static String timeFormatMOHDay() {
		return timeFormatMOHDay.format(now());
	}

	/**
	 * 获得当前月
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm
	 *
	 * @return
	 * @throws ParseException
	 */
	public static String timeFormatYmdHm(String dateTime) throws ParseException {
		return timeFormatMOHDay.format(parseDatetime(dateTime));
	}


	/**
	 * 获得当前月
	 * <p>
	 * 日期时间格式yyyy-MM-dd
	 *
	 * @return
	 * @throws ParseException
	 */
	public static String timeFormatYmd(String dateTime) throws ParseException {
		return dateFormat.format(parseDatetime(dateTime));
	}


	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyyMMddHHmmssSSS
	 *
	 * @return
	 */
	public static String currentFileDatetime() {
		return filetimeFormat.format(now());
	}

	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String nowTimeDatetime() {
		return nowTimeFormat.format(now());
	}

	/**
	 * 格式化日期时间
	 * <p>
	 * 日期时间格式yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String formatTimeDatetime(Date date) {
		return nowTimeFormat.format(date);
	}

	/**
	 * 获得当前日期
	 * <p>
	 * 日期时间格式yyyyMMdd
	 *
	 * @return
	 */
	public static String nowDatetime() {
		return nowDateFormat.format(now());
	}

	/**
	 * 格式化日期时间
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String formatDatetime(Date date) {
		return datetimeFormat.format(date);
	}

	/**
	 * 格式化日期时间
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm
	 *
	 * @return
	 */
	public static String datetimeMinuteFormat(Date date) {
		return datetimeMinuteFormat.format(date);
	}
	/**
	 * 格式化日期时间
	 * 日期时间格式MM-dd HH:mm
	 * @return
	 */
	public static String timeFormatMOHDayHourMinuteStr(Date date) {
		return timeFormatMOHDayHourMinute.format(date);
	}

	/**
	 * 格式化日期时间
	 * 日期时间格式MM-dd HH:mm
	 * @return
	 */
	public static String timeFormatYYYYMOHDayHourMinuteStr(Date date) {
		return timeFormatMOHDay.format(date);
	}

	/**
	 * 格式化日期时间
	 *
	 * @param date
	 * @param pattern
	 *            格式化模式，详见{@link }构造器
	 *            <code>SimpleDateFormat(String pattern)</code>
	 * @return
	 */
	public static String formatDatetime(Date date, String pattern) {
		return FastDateFormat.getInstance(pattern).format(date);
	}

	/**
	 * 获得当前日期
	 * <p>
	 * 日期格式yyyy-MM-dd
	 *
	 * @return
	 */
	public static String currentDate() {
		return dateFormat.format(now());
	}

	/**
	 * 格式化日期
	 * <p>
	 * 日期格式yyyy-MM-dd
	 *
	 * @return
	 */
	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}


	/**
	 * 格式化日期
	 * <p>
	 * 日期格式yyyy-MM-dd
	 *
	 * @return
	 */
	public static String formatDateStr(Date date) {
		return dateFormatStr.format(date);
	}
	/**
	 * 格式化日期
	 * <p>
	 * 日期格式MM-dd
	 *
	 * @return
	 */
	public static String formatDay(Date date) {
		return dayFormat.format(date);
	}

	/**
	 * 获得当前时间
	 * <p>
	 * 时间格式HH:mm:ss
	 *
	 * @return
	 */
	public static String currentTime() {
		return timeFormat.format(now());
	}

	/**
	 * 格式化时间
	 * <p>
	 * 时间格式HH:mm:ss
	 *
	 * @return
	 */
	public static String formatTime(Date date) {
		return timeFormat.format(date);
	}


	/**
	 * 格式化时间
	 * <p>
	 * 时间格式HH:mm
	 *
	 * @return
	 */
	public static String minuteFormatTime(Date date) {
		return minuteFormatTime.format(date);
	}
	/**
	 * 获得当前时间的<code>java.util.Date</code>对象
	 *
	 * @return
	 */
	public static Date now() {
		return new Date();
	}

	public static Calendar calendar() {
		Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		return cal;
	}

	/**
	 * 获得当前时间的毫秒数
	 * <p>
	 * 详见{@link System#currentTimeMillis()}
	 *
	 * @return
	 */
	public static long millis() {
		return System.currentTimeMillis();
	}

	/**
	 *
	 * 获得当前Chinese月份
	 *
	 * @return
	 */
	public static int month() {
		return calendar().get(Calendar.MONTH) + 1;
	}

	/**
	 * 获得月份中的第几天
	 *
	 * @return
	 */
	public static int dayOfMonth() {
		return calendar().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 今天是星期的第几天
	 *
	 * @return
	 */
	public static int dayOfWeek() {
		return calendar().get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 今天是年中的第几天
	 *
	 * @return
	 */
	public static int dayOfYear() {
		return calendar().get(Calendar.DAY_OF_YEAR);
	}

	/**
	 *判断原日期是否在目标日期之前
	 *
	 * @param src
	 * @param dst
	 * @return
	 */
	public static boolean isBefore(Date src, Date dst) {
		return src.before(dst);
	}

	/**
	 *判断原日期是否在目标日期之后
	 *
	 * @param src
	 * @param dst
	 * @return
	 */
	public static boolean isAfter(Date src, Date dst) {
		return src.after(dst);
	}

	/**
	 *判断两日期是否相同
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isEqual(Date date1, Date date2) {
		return date1.compareTo(date2) == 0;
	}

	/**
	 * 判断某个日期是否在某个日期范围
	 *
	 * @param beginDate
	 *            日期范围开始
	 * @param endDate
	 *            日期范围结束
	 * @param src
	 *            需要判断的日期
	 * @return
	 */
	public static boolean between(Date beginDate, Date endDate, Date src) {
		return beginDate.before(src) && endDate.after(src);
	}

	/**
	 * 获得当前月的最后一天
	 * <p>
	 * HH:mm:ss为0，毫秒为999
	 *
	 * @return
	 */
	public static Date lastDayOfMonth() {
		Calendar cal = calendar();
		cal.set(Calendar.DAY_OF_MONTH, 0); // M月置零
		cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
		cal.set(Calendar.MINUTE, 0);// m置零
		cal.set(Calendar.SECOND, 0);// s置零
		cal.set(Calendar.MILLISECOND, 0);// S置零
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1
		cal.set(Calendar.MILLISECOND, -1);// 毫秒-1
		return cal.getTime();
	}

	/**
	 * 获得当前月的第一天
	 * <p>
	 * HH:mm:ss SS为零
	 *
	 * @return
	 */
	public static Date firstDayOfMonth() {
		Calendar cal = calendar();
		cal.set(Calendar.DAY_OF_MONTH, 1); // M月置1
		cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
		cal.set(Calendar.MINUTE, 0);// m置零
		cal.set(Calendar.SECOND, 0);// s置零
		cal.set(Calendar.MILLISECOND, 0);// S置零
		return cal.getTime();
	}

	private static Date weekDay(int week) {
		Calendar cal = calendar();
		cal.set(Calendar.DAY_OF_WEEK, week);
		return cal.getTime();
	}

	/**
	 * 获得周五日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 *
	 * @return
	 */
	public static Date friday() {
		return weekDay(Calendar.FRIDAY);
	}

	/**
	 * 获得周六日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 *
	 * @return
	 */
	public static Date saturday() {
		return weekDay(Calendar.SATURDAY);
	}

	/**
	 * 获得周日日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 *
	 * @return
	 */
	public static Date sunday() {
		return weekDay(Calendar.SUNDAY);
	}

	public static String getFirstWorkDate() {
		return dateFormat.format(weekDay(Calendar.MONDAY));
	}

	public static String getLastWorkDate() {
		return dateFormat.format(weekDay(Calendar.FRIDAY));
	}

	/**
	 * 将字符串日期时间转换成java.util.Date类型
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 *
	 * @param datetime
	 * @return
	 */
	public static Date parseDatetime(String datetime) throws ParseException {
		return datetimeFormat.parse(datetime);
	}

	public static Date parseDatetimeNew(String datetime) {
		Date result = null;
		try{
			result = datetimeFormat.parse(datetime);
		}catch (Exception e){

		}
		return result;
	}

	/**
	 * 将字符串日期转换成java.util.Date类型
	 *<p>
	 * 日期时间格式yyyy-MM-dd
	 *
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String date) throws ParseException {
		return dateFormat.parse(date);
	}

	public static Date toDateLinux(String str){
		Date date = new Date();
		String[] parts = str.split("-");
		if(parts.length >= 3){
			int years = Integer.parseInt(parts[0]);
			int months = Integer.parseInt(parts[1]) - 1;
			int days = Integer.parseInt(parts[2]);
			String[] times = parts[3].split(":");
			int hours = Integer.parseInt(times[0]);
			int minutes = Integer.parseInt(times[1]);
			int seconds = Integer.parseInt(times[2]);
			GregorianCalendar gc = new GregorianCalendar(years,months, days,hours,minutes,seconds);
			date = gc.getTime();
		}
		return date;
	}

	/**
	 *判断是否是Linux环境的方法
	 * */
	public static boolean isLinuxOS() {
		Properties props = System.getProperties();
		String sysName = props.getProperty("os.name").toLowerCase();
		if (sysName.contains("linux")) {
			return true;
		}
		return false;
	}

	/**
	 * 将字符串日期转换成java.util.Date类型
	 *<p>
	 * 时间格式 HH:mm:ss
	 *
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date parseTime(String time) throws ParseException {
		return timeFormat.parse(time);
	}

	/**
	 * 根据自定义pattern将字符串日期转换成java.util.Date类型
	 *
	 * @param datetime
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDatetime(String datetime, String pattern)
			throws ParseException {

		return FastDateFormat.getInstance(pattern).parse(datetime);
	}

	/**
	 * 方法名：getCurrentDateAlter
	 * 描述：获取当前日期的前几天、后几天
	 * @参数：   day  int   加减天数
	 * @返回值类型： String
	 * @创建时间：  2014-7-23
	 * @创建者：陈威桦
	 * @变更记录：2014-7-23上午11:02:15 by
	 */
	public static String getCurrentDateAlter(int day) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, day);    //得到前一天
        String  yestedayDate = FastDateFormat.getInstance("yyyy-MM-dd").format(calendar.getTime());
		return yestedayDate;
	}

	public static String getCurrentDateAlter(String date,int day) throws ParseException {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.setTime(dateFormat.parse(date));
        calendar.add(Calendar.DATE, day);    //得到前一天
        String  yestedayDate = FastDateFormat.getInstance("yyyy-MM-dd").format(calendar.getTime());
		return yestedayDate;
	}

	public static String getCurrentDateTimeAlter(String date,int day) throws ParseException {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.setTime(datetimeFormat.parse(date));
		calendar.add(Calendar.DATE, day);    //得到前一天
		String  yestedayDate = datetimeFormat.format(calendar.getTime());
		return yestedayDate;
	}

	public static Date getCurrentDateTimeAlter(Date date,int day){
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);    //得到前一天
		return calendar.getTime();
	}
	/**
	 * 方法名：getCurrentDateAlter
	 * 描述：获取指定时间的前几分钟、后几分钟
	 * @参数：   dateTime  指定时间
	 * @参数：   minute  int   加减分钟
	 * @返回值类型： String
	 * @创建时间：  2015-11-27
	 * @创建者：张琳飞
	 * @变更记录：2015-11-27上午11:20:15 by
	 */
	public static String getCurrentDateMinuteAlter(String dateTime, int minute) {
		String changeDateMinute = "";
		Date dateMinute = new Date();
		try {
			dateMinute = datetimeFormat.parse(dateTime);
			changeDateMinute = datetimeFormat.format(dateMinute.getTime() + minute * 60000);
		} catch (ParseException e) {
			log.error("dateTime:"+dateTime+",minuteminute"+minute+"_"+e.getMessage(),e);
			e.printStackTrace();
		}
		return changeDateMinute;
	}

	public static String dayOfWeekInChines(){
		int dayOfWeek = DateUtil.dayOfWeek();
		return DateUtil.dayOfWeek[dayOfWeek-1];
	}

	public static String getWeek(String date) throws ParseException{
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateFormat.parse(date));
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
			week_index = 0;
		}
		return dayOfWeek[week_index];
	}

	public static String getWeekDate(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
			week_index = 0;
		}
		return dayOfWeek[week_index];
	}

	/**
	 * 获取当前日期的前几月、后几月
	 * @param date
	 * @param month int 加减月数
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentYmAlter(String date,int month) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		try {
			calendar.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			log.error("date:"+date+",month"+month+"_"+e.getMessage(),e);
		}
		calendar.add(Calendar.MONTH, month);    //得到前后几月
		String DateMonth = dateDayFormat.format(calendar.getTime());
		return DateMonth;
	}

	/**
	 * 获取当前日期的前几月、后几月
	 * @param date
	 * @param month int 加减月数
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentYmdAlter(String date,int month) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		try {
			calendar.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			log.error("date:"+date+",month"+month+"_"+e.getMessage(),e);
		}
		calendar.add(Calendar.MONTH, month);    //得到前后几月
		String DateMonth = dateFormat.format(calendar.getTime());
		return DateMonth;
	}

	/**
	 * 获取指定日期的前几小时、后几小时
	 * @param date
	 * @param hours int 加减小时
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentHoursAlter(String date, int hours) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		try {
			calendar.setTime(datetimeFormat.parse(date));
		} catch (ParseException e) {
			log.error("date:"+date+",hours:"+hours+"_"+e.getMessage(),e);
		}
		calendar.add(Calendar.HOUR, hours);    //得到前后小时
		String DateHours = datetimeFormat.format(calendar.getTime());
		return DateHours;
	}

	/**
	 * 获取月份天数
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static int getMonthDays(String date) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		try {
			calendar.setTime(dateDayFormat.parse(date));
		} catch (ParseException e) {
			log.error("date:"+date+"_"+e.getMessage(),e);
		}
		int day = calendar.getActualMaximum(Calendar.DATE);
		return day;
	}

	public static List<String> getFiveWorkDayFromTody(String date1,String date2) throws ParseException{
		List<String> resList = new ArrayList<String>();
		if(date1==null){
			date1 = DateUtil.getCurrentDateAlter(-7);
		}

		Date startDate = dateFormat.parse(date1);
		if(date2==null){
			date2 = DateUtil.currentDate();
		}
		Date today = dateFormat.parse(date2);
		while (!startDate.after(today)) {
			String week = getWeek(dateFormat.format(startDate));
			if (!(dayOfWeek[0].equals(week)||dayOfWeek[6].equals(week))) {
				resList.add(week);
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			cal.add(Calendar.DAY_OF_MONTH, 1);
			startDate = cal.getTime();
		}
		return resList;
	}

	public static List<String> getFiveWorkDayStrFromTody(String date1,String date2) throws ParseException{
		List<String> resList = new ArrayList<String>();
		if(date1==null){
			date1 = DateUtil.getCurrentDateAlter(-7);
		}
		Date startDate = dateFormat.parse(date1);
		if(date2==null){
			date2 = DateUtil.currentDate();
		}
		Date today = dateFormat.parse(date2);
		while (!startDate.after(today)) {
			String week = getWeek(dateFormat.format(startDate));
			if (!(dayOfWeek[0].equals(week)||dayOfWeek[6].equals(week))) {
				resList.add(dateFormat.format(startDate));
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			cal.add(Calendar.DAY_OF_MONTH, 1);
			startDate = cal.getTime();
		}
		return resList;
	}

	/**
     * 计算两个日期之间相差的天数
     * @param startDateStr 较小的时间
     * @param endDateStr  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String startDateStr,String endDateStr) throws ParseException
    {
    	Date startDate = dateFormat.parse(startDateStr);
    	Date endDate = dateFormat.parse(endDateStr);

       return daysBetween(startDate,endDate);
    }

	/**
     * 计算两个日期之间相差的天数
     * @param startDate 较小的时间
     * @param endDate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date startDate,Date endDate) throws ParseException
    {
		FastDateFormat sdf=FastDateFormat.getInstance("yyyy-MM-dd");
        startDate=sdf.parse(sdf.format(startDate));
        endDate=sdf.parse(sdf.format(endDate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

       return Integer.parseInt(String.valueOf(between_days));
    }

	/**
	 * 计算两个日期之间相差的分钟数
	 * @param startDate 较小的时间
	 * @param endDate  较大的时间
	 * @return 相差分钟数
	 * @throws ParseException
	 */
	public static int minutesBetween(Date startDate,Date endDate) {
		int minuteBw = 0;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(endDate);
			long time2 = cal.getTimeInMillis();
			long betweenMinutes=(time2-time1)/(1000*60);
			minuteBw = Integer.parseInt(String.valueOf(betweenMinutes));
		} catch (Exception e) {
			log.error("startDate:"+startDate+",endDate:"+endDate+"_"+e.getMessage(),e);
		}
		return minuteBw;
	}

	public static String getWeekOfNum(String date) throws ParseException{
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateFormat.parse(date));
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
			week_index = 0;
		}
		return ""+ week_index;
	}

    /**
     * 获取指定时间毫秒数
     * @param time
     * @return
     * @throws ParseException
     */
    public static long getTimeMillisecond(String time) {
    	Date dTime = new Date();
		try {
			dTime = datetimeFormat.parse(time);
		} catch (ParseException e) {
			log.error("time:"+time+"_"+e.getMessage(),e);
		}
    	return dTime.getTime();
    }

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
	/*	String time = DateUtil.getCurrentDateAlter(1) + "-00:00:00";
		Date date = toDateLinux(time);
		//String str = "天天向上asd123";
		//System.out.println(str.length());
		boolean linuxOS = isLinuxOS();
		System.out.println(linuxOS);

		String s = timeFormatMOHDayHourMinuteStr(new Date());

		System.out.println(s);*/
		//FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        //System.out.println(DateUtil.compareDate(sdf.parse("2021-11-06 00:22:33"),sdf.parse("2021-11-10 06:15:30"),3));
		//System.out.println(dateFormat.format(getWeekStartDate()));

		//Date dateYmdAlter = getDateYmdAlter(new Date(),24);
		//String format = sdf.format(dateYmdAlter);
		//System.out.println("测试时间----"+format);

		//int i = 305456;
		/*String s = sumSecondToTime(i);
		System.out.println(s);*/
		//System.out.println(DateUtil.compareDate(sdf.parse("2021-11-08 00:22:33"),sdf.parse("2021-11-07 00:22:33")));
		//String dayTime = DateUtil.formatDate(new Date()) + " 23:59:59";

		//System.out.println(DateUtil.parseDatetime(dayTime).compareTo(new Date()));
		/*Date now = new Date();
		FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
		Date streamTime = sdf.parse("2022-06-17 19:25:00");
		if(DateUtil.compareDate(new Date(now.getTime() + 40*60*1000 ),streamTime) >= 0 && DateUtil.compareDate(new Date(now.getTime() + 30*60*1000 ),streamTime) <= 0 ){
			//baseLiveStreamPlanService.sendStartedBroadcastingMessage(baseLiveStreamPlan);
			System.out.println(1111);
		}*/
		//String signInDate = "2022-06-24";
		/*Date d = new Date();
		System.out.println(DateUtil.getChineseAbbreviationsDatetimeFormatMdHm(d));*/
	/*	FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
		System.out.println(DateUtil.computeBetweenDateDays(new Date(),sdf.parse("2022-07-13 23:22:33")));*/
		//System.out.println(dateFormat.format(getWeekStartDate()));
		FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
		System.out.println(DateUtil.minutesBetween(sdf.parse("2022-07-17 16:00:33"),new Date()) >= 30);
	}


	/**
	 * 某一个月第一天和最后一天
	 * @param date
	 * @return
	 */
	public static Map<String, String> getFirstday_Lastday_Month(Date date) {
		FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		//calendar.add(Calendar.MONTH, -1);
		Date theDate = calendar.getTime();

		//上个月第一天
		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(theDate);
		gcLast.set(Calendar.DAY_OF_MONTH, 1);
		String day_first = df.format(gcLast.getTime());
		StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
		day_first = str.toString();

		//上个月最后一天
		calendar.add(Calendar.MONTH, 1);    //加一个月
		calendar.set(Calendar.DATE, 1);        //设置为该月第一天
		calendar.add(Calendar.DATE, -1);    //再减一天即为上个月最后一天
		String day_last = df.format(calendar.getTime());
		StringBuffer endStr = new StringBuffer().append(day_last).append(" 23:59:59");
		day_last = endStr.toString();

		Map<String, String> map = new HashMap<String, String>();
		map.put("first", day_first);
		map.put("last", day_last);
		return map;
	}

	/**
	 *
	 * @param currentDate 现在日期
	 * @param birthday 出生日期
	 * @return
	 * @throws Exception
	 */
	public static String getAge(String currentDate, String birthday) {
		String age = "";
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(dateFormat.parse(currentDate));
			int theYear = calendar.get(Calendar.YEAR);
			int theMonth = calendar.get(Calendar.MONTH) + 1;
			int theDay = calendar.get(Calendar.DATE);
			int maxDay = calendar.getActualMaximum(Calendar.DATE);

			calendar.setTime(dateFormat.parse(birthday));
			int birthYear = calendar.get(Calendar.YEAR);
			int birthMonth = calendar.get(Calendar.MONTH) + 1;
			int birthDay = calendar.get(Calendar.DATE);
			int year = theYear - birthYear;
			int month = theMonth - birthMonth;
			int day = theDay - birthDay;
			if (theMonth <= birthMonth) {
				if (theMonth == birthMonth) {
					if (theDay < birthDay) {
						year--;
					}
				} else {
					year--;
				}
			}
			if (month < 0) {
				month = 12 + month;
			}
			if (day < 0) {
				month--;
				day = maxDay + day;
			}
			age = year + "岁" + month + "个月" + day + "天";
		} catch (ParseException e) {
			log.error("currentDate:"+currentDate+",birthday:"+birthday+"_"+e.getMessage(),e);
		}
		return age;
	}

	/** 日期进行比较
	 * @param DATE1
	 * @param DATE2
	 * @return 1:date1>date2;-1:date1<date2;0:date1=date2
	 */
	public static int compareDate(String DATE1, String DATE2) {
        try {
            Date dt1 = dateFormat.parse(DATE1);
            Date dt2 = dateFormat.parse(DATE2);
            return dt1.compareTo(dt2);
        } catch (Exception exception) {
			log.error("DATE1:"+DATE1+",DATE2:"+DATE2+"_"+exception.getMessage(),exception);
        }
        return 0;
    }


	public static int compareDateinfo1(String DATE1, String DATE2) {
        try {
            Date dt1 = onlyTimeFormat.parse(DATE1);
            Date dt2 = onlyTimeFormat.parse(DATE2);
            return dt1.compareTo(dt2);
        } catch (Exception exception) {
			log.error("DATE1:"+DATE1+",DATE2:"+DATE2+"_"+exception.getMessage(),exception);
        }
        return 0;
    }

	public static int compareDateinfo(String DATE1, String DATE2) {
		try {
			Date dt1 = datetimeFormat.parse(DATE1);
			Date dt2 = datetimeFormat.parse(DATE2);
			return dt1.compareTo(dt2);
		} catch (Exception exception) {
			log.error("DATE1:"+DATE1+",DATE2:"+DATE2+"_"+exception.getMessage(),exception);
		}
		return 0;
	}


	public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return calendar.getTime();
    }

	public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return calendar.getTime();
    }

	public static Date getNext(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static String getDay(Date date){
		String dateStr = "";
		try{
			FastDateFormat df =  FastDateFormat.getInstance("yyyy-MM-dd");
			dateStr = df.format(date);
		}catch(Exception e){
			log.error("date:"+date+"_"+e.getMessage(),e);
		}
		return dateStr;
	}

  public static Date getWeekStartDate(){
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    Date date = cal.getTime();
    return date;
  }

  /**
   * 获取某年第一天日期
   * @param year 年份
   * @return Date
   */
  public static Date getYearFirst(int year){
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, year);
    Date currYearFirst = calendar.getTime();
    return currYearFirst;
  }

  /**
   * 获取某年最后一天日期
   * @param year 年份
   * @return Date
   */
  public static Date getYearLast(int year){
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, year);
    calendar.roll(Calendar.DAY_OF_YEAR, -1);
    Date currYearLast = calendar.getTime();

    return currYearLast;
  }

  /**
   * 获取当年的第一天
   * @param
   * @return
   */
  public static Date getCurrYearFirst(){
    Calendar currCal=Calendar.getInstance();
    int currentYear = currCal.get(Calendar.YEAR);
    return getYearFirst(currentYear);
  }

  /**
   * 获取当年的最后一天
   * @param
   * @return
   */
  public static Date getCurrYearLast(){
    Calendar currCal=Calendar.getInstance();
    int currentYear = currCal.get(Calendar.YEAR);
    return getYearLast(currentYear);
  }

  public static Date getZeroDate(){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date zero = calendar.getTime();
    return  zero;
  }
  public static String dateDayFormat(Date date,String flag){
    String dateStr = "";
    if("yyyy".equalsIgnoreCase(flag)){
      dateStr =  nowDateFormatYyyy.format(date);
    }else if("yyyymm".equalsIgnoreCase(flag)){
      dateStr =  nowDateFormatYyyyMm.format(date);
    }else if("yymmdd".equalsIgnoreCase(flag)){
      dateStr =  nowDateFormatYy.format(date);
    }else if("ymmdd".equalsIgnoreCase(flag)){
      dateStr =  nowDateFormatY.format(date);
    } else{
      dateStr =  nowDateFormat.format(date);
    }
    return dateStr;
  }

  public static boolean isSameDay(final Date date1, final Date date2) {
    if(date1 == null || date2 == null) {
      throw new IllegalArgumentException("The date must not be null");
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isSameDay(cal1, cal2);
  }

  public static boolean isSameDay(final Calendar cal1, final Calendar cal2) {
    if (cal1 == null || cal2 == null) {
      throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
      cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
      cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
  }

	/**
	 * 获取当前日期的前几月、后几月
	 * @param month int 加减月数
	 * @return
	 * @throws ParseException
	 */
	public static Date getCurrentYmdAlter(int month) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.add(Calendar.MONTH, month);    //得到前后几月
		return calendar.getTime();
	}

	/**
	 * 获取当前日期的前几月、后几月
	 * @param month int 加减月数
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateYmdAlter(Date date,int month) {
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);    //得到前后几月
		return calendar.getTime();
	}


	/** 日期进行比较
	 * @param DATE1
	 * @param DATE2
	 * @return 1:date1>date2;-1:date1<date2;0:date1=date2
	 */
	public static int compareDate(Date DATE1, Date DATE2) {
		try {
			return DATE1.compareTo(DATE2);
		} catch (Exception exception) {
			log.error("DATE1:"+DATE1+",DATE2:"+DATE2+"_"+exception.getMessage(),exception);
		}
		return 0;
	}

	/**
	 * 根据日期转换为星期
	 * @Author: wlxiang
	 * @Date: 2021/10/14 11:09
	 * @param date:
	 * @return: java.lang.String
	 **/
	public static String getWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
			week_index = 0;
		}
		return dayOfWeek[week_index];
	}

	/**
	 * 获取最大的有效期时间（终身）
	 * @return
	 */
	public static Date getMaxDate(){
		Date date = new Date(Long.MAX_VALUE);
		try {
			date = DateUtil.parseDatetime("2099-12-31 00:00:00");
		}catch (Exception e){
			e.printStackTrace();
			date = DateUtil.getDateYmdAlter(new Date(),100);
		}
		return date;
	}

	/**
	 * 判断个日期间隔时间是否大于传入的天数
	 * @Author: wlxiang
	 * @Date: 2021/11/26 13:59
	 * @param minDate:
	 * @param maxDate:
	 * @param days:
	 * @return: boolean
	 **/
	public static boolean compareDate(Date minDate,Date maxDate,int days){
		try {
			String minStr = DateUtil.formatDate(minDate);
			String maxStr = DateUtil.formatDate(maxDate);
			int intervalDays = DateUtil.daysBetween(minStr,maxStr);
			return (intervalDays - days) > 0;
		} catch (Exception exception) {
			log.error("minDate:"+minDate+",maxDate:"+maxDate+"_"+exception.getMessage(),exception);
		}
		return false;
	}

	/**
	 * 判断两个日期间隔时间
	 * @Author: wlxiang
	 * @Date: 2021/11/26 13:59
	 * @param minDate:
	 * @param maxDate:
	 * @return: int
	 **/
	public static int computeBetweenDateDays(Date minDate,Date maxDate){
		try {
			String minStr = DateUtil.formatDate(minDate);
			String maxStr = DateUtil.formatDate(maxDate);
			return  DateUtil.daysBetween(minStr,maxStr);
		} catch (Exception exception) {
			log.error("minDate:"+minDate+",maxDate:"+maxDate+"_"+exception.getMessage(),exception);
		}
		return -1;
	}

	/**
	 * 把秒数转换为日期格式字符串
	 * @param sumSecond
	 * @return
	 */
	public static String sumSecondToTime(int sumSecond) {
		if(sumSecond <= 0){
			return "00:00:00";
		}
		int h = sumSecond/3600;
		int m = (sumSecond-h*3600)/60;
		int s = sumSecond - h*3600-m*60;
		String time = "%02d:%02d:%02d";
		time = String.format(time,h,m,s);
		return time;
	}

	/**
	 * 返回 2022年03月10日 15:12
	 * @Author: wlxiang
	 * @Date: 2022/3/10 15:12
	 * @param date:
	 * @return: java.lang.String
	 **/
	public static String getChineseAbbreviationsDatetimeFormat(Date date){
		return chineseAbbreviationsDatetimeFormat.format(date);
	}

	/**
	 * 返回 03月10日 15:12
	 * @Author: wlxiang
	 * @Date: 2022/3/10 15:12
	 * @param date:
	 * @return: java.lang.String
	 **/
	public static String getChineseAbbreviationsDatetimeFormatMdHm(Date date){
		return chineseAbbreviationsDatetimeFormatMdHm.format(date);
	}
	/**
	 * 根据生日获取年龄
	 * @Author: wlxianng
	 * @Date: 2022/2/17 10:42
	 * @param birthday:
	 * @return: java.lang.String
	 **/
	public static Integer getAge (String birthday){
		Date date = new Date();
		Integer age = 0;
		try{
			// 出生日期年份
			String year = birthday.substring(0, 4);
			// 出生日期年份的月份
			String yue = birthday.substring(5, 7);
			// 当前年份
			String fyear = dateFormat.format(date).substring(0, 4);
			// 当前月份
			String fyue = dateFormat.format(date).substring(5, 7);
			// 当前月份大于用户出身的月份表示已过生日
			if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
				age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
				// 当前用户还没过生日
			} else {
				age = Integer.parseInt(fyear) - Integer.parseInt(year);
			}
		}catch (Exception e){
			age = 0;
		}
		return age;
	}

/*	*//**
	 * 获取指定格式的昨天日期
	 * @param pattern
	 * @return
	 */
	public static String getYesterday(String pattern) {

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE,-1);
		Date time = calendar.getTime();
		String yesterday =  FastDateFormat.getInstance(pattern).format(time);
		return yesterday;
	}

	/**
	 * 获取当前时间到凌晨12点的秒数
	 * @return
	 */
	public static Long getSecondsNextEarlyMorning() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
	}

	/**
	 * @Description 获取周岁
	 * @Author wlxiang
	 * @Date 13:57 2022/8/7
	 * @Param [currentDate, birthday]
	 * @return java.lang.String
	 **/
	public static int getMajorityAge(String currentDate, String birthday) {
		Integer age = 0;
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(dateFormat.parse(currentDate));
			int theYear = calendar.get(Calendar.YEAR);
			int theMonth = calendar.get(Calendar.MONTH) + 1;
			int theDay = calendar.get(Calendar.DATE);
			int maxDay = calendar.getActualMaximum(Calendar.DATE);

			calendar.setTime(dateFormat.parse(birthday));
			int birthYear = calendar.get(Calendar.YEAR);
			int birthMonth = calendar.get(Calendar.MONTH) + 1;
			int birthDay = calendar.get(Calendar.DATE);
			int year = theYear - birthYear;
			if (theMonth <= birthMonth) {
				if (theMonth == birthMonth) {
					if (theDay < birthDay) {
						year--;
					}
				} else {
					year--;
				}
			}

			age = year;
		} catch (ParseException e) {
			log.error("currentDate:"+currentDate+",birthday:"+birthday+"_"+e.getMessage(),e);
		}
		return age;
	}
}
